package com.aixin.project.handler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.aixin.project.data.CommonConstants;
import com.aixin.project.data.Course;
import com.aixin.project.data.Expert;
import com.aixin.project.db.CourseDao;
import com.aixin.project.db.ExpertDao;
import com.aixin.project.util.AxMemcached;


public class ExpertHandler {
	 private static int num=4;
	
	  public static Expert find(long uid){
		    Expert expert=(Expert) AxMemcached.get(CommonConstants.MKEY_EXPERT + uid); 
		    if(expert==null){
		    	expert=ExpertDao.find(uid);
		    	if(expert!=null)
		    		AxMemcached.set(CommonConstants.MKEY_EXPERT+uid,expert,new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_WEEK*1000));
		    }
 	        return expert;
	    }
	  
	  public static Expert find(String username,String password){
	    	return ExpertDao.find(username,password);
	    }
	  
	  public static Expert findByUid(long uid){
		  return ExpertDao.find(uid);
	    }
	  
	  public static List<Expert> expertList(int page){
          List<Expert> ret = (List<Expert>) AxMemcached.get(CommonConstants.MKEY_EXPERT_PAGE_LIST + page);
          if(ret == null){
              ret = ExpertDao.expertList(num, page);
              if(ret !=null && ret.size()>0)
                  AxMemcached.set(CommonConstants.MKEY_EXPERT_PAGE_LIST + page, ret, new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_DAY*1000));
          }
	        return ret;
	    }
	  
	  public static int getMaxpage(){
		  int maxpage=1;
		  int totalResult=0;
		  Object total=ExpertDao.totalRs();

		  if(total!=null){
			  totalResult=Integer.parseInt(total.toString());
			  maxpage=totalResult/num;
			  maxpage=totalResult%num==0?maxpage:maxpage+1;  
		  }
		
		  return maxpage;
			  
	  }
	 
	  public static List<Course> expertComingCourses(long expertid){
          List<Course> ret = null;
		  List<Integer> courseIdList= (List<Integer>) AxMemcached.get(CommonConstants.MKEY_E_COMING_COURSE_LIST + expertid);
		  if(courseIdList==null){
              courseIdList= CourseDao.commingCourseIdByEid(expertid);
		    if(courseIdList!=null && courseIdList.size()>0){                
		    	AxMemcached.set(CommonConstants.MKEY_E_COMING_COURSE_LIST+expertid,courseIdList,new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_HOUR*1000));
            }
		  }
          if(courseIdList!=null && courseIdList.size()>0){
              ret = new ArrayList<Course>();
              for(Integer id:courseIdList){
                  Course c = CourseHandler.find(id);
                  if(c!=null)
                      ret.add(c);
              }
          }
		  return ret;
	  }
	  
	  public static List<Expert> hallexpertList(){
          List<Expert> ret = null;
		  List<Integer> eids = (List<Integer>) AxMemcached.get(CommonConstants.MKEY_HALL_EXPERTLIST);
		  if(null == eids){
              eids = ExpertDao.allexpertIdList();
              if(eids != null && eids.size()>0)
			    AxMemcached.set(CommonConstants.MKEY_HALL_EXPERTLIST, eids, new Date(CommonConstants.MEMCACHED_SESSION_TIMEOUT_WEEK*1000));
		  }
          if(eids!=null && eids.size()>0){
              ret = new ArrayList<Expert>();
              for(Integer eid:eids){
                  Expert e = find(eid);
                  if(e == null) continue;
                  ret.add(e);
              }
          }
		  return ret;
	  }
	  
	  public static List<Expert> expertList(){
		  return ExpertDao.allexpertList();
	  }
	  
	  public static int update(long expid,int status){
		  return ExpertDao.updateStatus(expid,status);
	  }
	  
	  public static int updatepwd(long userid,String pwd){
	    	return ExpertDao.updatepwd(userid,pwd);
	    	//AxMemcached.delete(CommonConstants.mk);
	    }
	  
	  public static long addExpert(Expert expert){
		  return ExpertDao.addExpert(expert);
	  }
	  
	  public static void removeExpert(String id){
		  ExpertDao.deleteExpert(id);
	  }

    public static void clearExpertPageCache(){
        int maxpage = getMaxpage();
        for(int i=1;i<=maxpage;i++)
            AxMemcached.delete(CommonConstants.MKEY_EXPERT_PAGE_LIST+i);
    }
    
    public static List<Expert> getAdminExperts(int page,int psize,int status){
    	return ExpertDao.listPagenation(page,psize,status);
    }
    
    public static long count(int status){
        return ExpertDao.count(status);
    }
}
